﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShareMarking.Systems.Service;
using Common.Utilities.Excel;
using System.Text.RegularExpressions;
using Common.Utilities.Excel.Import;
using System.Data;
using ShareMarking.Service;
using ShareMarking.Entity;
using ShareMarking.Interface;
using System.Configuration;
using MySoft.Data;
namespace ShareMarking.Web.admin.EnterPrise.Employee
{
    public partial class moretoon : ManagePage
    {
        private string httpstr = ConfigurationManager.AppSettings["vigohttp"];
        private string signsecret = ConfigurationManager.AppSettings["signscrect"];

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        #region 导入NC扩展信息==================================

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //获取文件路径
            var filepath = Server.MapPath(this.txt_filename.Text.Trim());
            ExcelUtilities util = new ExcelUtilities();
            Excel excel = null;
            //获取配置
            var result = util.ImportExcel(filepath, "ncactimport", out excel);

            if (result.Result == ExcelResultType.Succeed)
            {
                var updatecount = 0;//成功条数
                int errorCount = 0;//失败条数
                var datasource = excel.DataSet;
                if (datasource.Tables.Count > 0)
                {
                    //先删除
                    DbSession.Default.FromSql("DELETE ext_ncuser").Execute();

                    foreach (DataRow row in datasource.Tables[0].Rows)
                    {
                        #region 循环判断更新
                        if (!row[0].ToString().IsNullOrEmpty())
                        {
                            try
                            {
                                #region 更新二维码
                                var entity = nc_ncuserService.Instance.Get_SingleEntity_byWhere(ShareMarking.Entity.nc_ncuser._.nc_code == row[0].ToString());
                                if (entity != null)
                                {
                                    var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                                        qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
                                        && qr_QrCodeRelate._.rel_Code == entity.nc_code);
                                    if (relate != null)
                                    {
                                        var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
                                        if (qrcode != null)
                                        {
                                            #region 更新二维码

                                            #region 编辑图文信息
                                            var extra = new List<extra>() { 
                                                new extra { 
                                                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                                    shortdesc = "一键注册，终身礼遇", 
                                                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+entity.nc_code+"$openId=####", 
                                                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                                },
                                                new extra{
                                                    title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
                                                    shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
                                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                                    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                                },
                                                new extra{
                                                    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
                                                    shortdesc = "惊喜连连，准备好了么？", 
                                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                                    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                                }
                                            };
                                            var message = new message2()
                                            {
                                                id = qrcode.id.ToString(),
                                                title = entity.name,
                                                remark = entity.name + "的二维码",
                                                sacode = entity.name + "的二维码",
                                                extra = extra
                                            };
                                            #endregion

                                            var vigo = new Qrcode_vigo(this.httpstr, this.signsecret);
                                            vigo.UpdateQrcode(message);
                                            #endregion

                                            ComLib.LogLib.Log4NetBase.Log("3、====NC多图文：解绑：：NCCode：：" + entity.nc_code);
                                            updatecount += 1;
                                        }
                                    }
                                    else
                                    {
                                        if (entity.state == "离职员工")
                                        {
                                            ComLib.LogLib.Log4NetBase.Log("3、====NC多图文：解绑，员工离职，没有二维码：：NCCode：：" + entity.nc_code);
                                        }
                                    }
                                }
                                else
                                {
                                    ComLib.LogLib.Log4NetBase.Log("3、====NC多图文：解绑的NC找不到：：NCCode：：" + row[0].ToString());
                                }
                                #endregion

                                #region 添加临时表，用户检验时查找表

                                if (entity != null)
                                {
                                    if (!ext_ncuserService.Instance.Get_IsExist_ByWhere(ext_ncuser._.nc_code == entity.nc_code))
                                    {
                                        ext_ncuser en = new ext_ncuser()
                                        {
                                            name = entity.name,
                                            nc_code = entity.nc_code
                                        };
                                        ext_ncuserService.Instance.Add_Entity(en);
                                    }

                                }

                                #endregion
                            }
                            catch (Exception ex)
                            {
                                ComLib.LogLib.Log4NetBase.Log("3、====NC多图文：解绑出错：：NCCode：：" + row[0].ToString());
                            }
                        }
                        else
                        {
                            errorCount += 1;//失败条数
                        }
                        #endregion
                    }
                }
                JscriptMsg("导入成功" + updatecount + "条，失败" + errorCount + "条！", "nc.aspx", "Success");
            }
            else
            {
                JscriptMsg("addressimport", "导入失败！请检查excel！", "", "Error");
            }
        }
        #endregion
    }
}